#include "FreeRTOS_demo.h"
#include "led.h"

#define START_TASK_PRIORITY 6
#define START_TASK_STACK_DEPTH 32
TaskHandle_t start_task_hander;
void Start_Task(void *pvParameters);

#define LED_TASK_PRIORITY 6
#define LED_TASK_STACK_DEPTH 64
TaskHandle_t led_task_hander;
void Led_Task(void *pvParameters);

// BaseType_t xTaskCreate
// (
//     TaskFunction_t pxTaskCode,                  /* 指向任务函数的指针 */
//     const char * const pcName,                  /* 任务名字，最大长度configMAX_TASK_NAME_LEN */
//     const configSTACK_DEPTH_TYPE usStackDepth,  /* 任务堆栈大小，默认单位4字节 */
//     void * const pvParameters,                  /* 传递给任务函数的参数 */
//     UBaseType_t uxPriority,                     /* 任务优先级，范围：0 ~ configMAX_PRIORITIES - 1 */
//     TaskHandle_t * const pxCreatedTask          /* 任务句柄，就是任务的任务控制块 */
// ) 

void FreeRTOS_Start(void)
{
    xTaskCreate((TaskFunction_t)Start_Task,
                (char *)"start_task",
                START_TASK_STACK_DEPTH,
                (void *)NULL,
                (UBaseType_t)START_TASK_PRIORITY,
                (TaskHandle_t *)start_task_hander);
    vTaskStartScheduler();
}

void Start_Task(void *pvParameters)
{
    taskENTER_CRITICAL();
    xTaskCreate((TaskFunction_t)Led_Task,
                (char *)"led_task",
                LED_TASK_STACK_DEPTH,
                (void *)NULL,
                (UBaseType_t)LED_TASK_PRIORITY,
                (TaskHandle_t *)led_task_hander);
    taskEXIT_CRITICAL();
    vTaskDelete(NULL);
}

void Led_Task(void *pvParameters)
{
    TickType_t xLastWakeTime = xTaskGetTickCount();
    const TickType_t xPeriod = pdMS_TO_TICKS(500);
    while (1)
    {
        LED_lamp_open();
        vTaskDelayUntil(&xLastWakeTime,xPeriod);
        LED_lamp_close();
        vTaskDelayUntil(&xLastWakeTime,xPeriod);
    }   
}
